import axios from '@/utils/http';
import base from './base';

export default {
  // 加载列表
  index(params) {
    return axios.get(
      `${base.sq}/products`,
      {
        params: {
          select_type: params.type, // 空间列表加载的类型
          page: params.page, // 第几页
          order_type: params.sort,
          order_action: params.order,
          user_id: params.userId, // 可以带上用户id查看别人拥有的空间
        },
      },
    );
  },
  // 创建空间
  create(params) {
    return axios.post(
      `${base.sq}/products`,
      {
        title: params.title,
        description: params.desc,
        ufile_key: params.avatarKey,
        is_open: params.public,
      },
    );
  },
  // 进入空间
  show(params) {
    return axios.get(
      `${base.sq}/products/${params.spaceId}`,
    );
  },
  // 更新空间
  update(params) {
    return axios.put(
      `${base.sq}/products/${params.spaceId}`,
      {
        title: params.title,
        description: params.desc,
        ufile_key: params.avatarKey,
        is_open: params.public,
        auto_accept: params.autoAccept,
        open_core: params.core,
      },
    );
  },
  delete(params) {
    return axios.delete(
      `${base.sq}/products/${params.spaceId}`,
    );
  },
  operate(params) {
    return axios.post(
      `${base.sq}/products/${params.spaceId}/operating`,
      {
        type: params.type,
        value: params.value,
      },
    );
  },
  // 空间侧边栏各个模块的统计数字
  asideCount(params) {
    return axios.get(
      `${base.sq}/products/${params.spaceId}/count`,
    );
  },
  // 获取空间邀请码
  joinKey(params) {
    return axios.post(
      `${base.sq}/products/${params.spaceId}/set_key`,
      {
        min: params.minute,
      },
    );
  },
  join(params) {
    return axios.post(
      `${base.sq}/products/join`,
      {
        key: params.joinKey,
        product_id: params.spaceId,
      },
    );
  },
  leave(params) {
    return axios.post(
      `${base.sq}/products/${params.spaceId}/leave`,
    );
  },
  // 获取社区邀请信息
  getInvites(params) {
    return axios.get(
      `${base.sq}/communities/0/community_invites`,
      {
        params: {
          product_id: params.spaceId,
        },
      },
    );
  },
  // 处理社区邀请
  handleInvite(params) {
    return axios.put(
      `${base.sq}/communities/${params.communityId}/community_invites/${params.inviteId}`,
      {
        product_id: params.spaceId,
        approve: params.state,
      },
    );
  },
  // 获取加入的空间
  joinedCommunities(params) {
    return axios.get(
      `${base.sq}/communities`,
      {
        params: {
          product_id: params.spaceId,
          type: 'product',
          page: params.page,
        },
      },
    );
  },
  // 退出社区
  leaveCommunity(params) {
    return axios.post(
      `${base.sq}/products/${params.spaceId}/community_out`,
      {
        community_id: params.communityId,
      },
    );
  },
  // 空间内的标签
  tags(params) {
    return axios.get(
      `${base.sq}/products/${params.spaceId}/tags`,
      {
        params: {
          search: params.search,
          tag_type: params.type, // ProductFile ProductIssues
        },
      },
    );
  },
  // 移交空间
  transfer(params) {
    return axios.post(
      `${base.sq}/products/${params.spaceId}/transfer`,
      {
        user_id: params.userId,
      },
    );
  },
};
